<!doctype html>
<html lang="zh-CN">
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    <meta name="referrer" content="no-referrer-when-downgrade">
    

    <title>Gorm连接数据库 | 早起的年轻人</title>
    <meta property="og:title" content="Gorm连接数据库 - 早起的年轻人">
    <meta property="og:type" content="article">
        
    <meta property="article:published_time" content='2021-09-13T10:04:07&#43;08:00'>
        
        
    <meta property="article:modified_time" content='2021-09-13T10:04:07&#43;08:00'>
        
    <meta name="Keywords" content="Flutter，golang,go语言,go语言笔记,飞雪无情,java,android,博客,项目管理,python,软件架构,公众号,小程序">
    <meta name="description" content="Gorm连接数据库">
        
    <meta name="author" content="luckly">
    <meta property="og:url" content="https://luckly.work/post/go_basic/gorm%E8%BF%9E%E6%8E%A5%E6%95%B0%E6%8D%AE%E5%BA%93/">
    <link rel="shortcut icon" href='/favicon.ico'  type="image/x-icon">

    <link rel="stylesheet" href='/css/normalize.css'>
    <link rel="stylesheet" href='/css/style.css'>
    <script type="text/javascript" src="//cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

    
    
    
        <link href="https://cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css" rel="stylesheet">
    
    
    
    
        <link rel="stylesheet" href='/css/douban.css'>
    
        <link rel="stylesheet" href='/css/other.css'>
    
</head>

<link rel="stylesheet"
      href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/styles/default.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/highlight.min.js"></script>
<body>
    <header id="header" class="clearfix">
    <div class="container">
        <div class="col-group">
            <div class="site-name ">
                
                    <a id="logo" href="https://luckly.work/">
                        早起的年轻人
                    </a>
                
                <p class="description">专注于Flutter、Android、Java、Go语言(golang)、移动互联网、项目管理、软件架构</p>
            </div>
            <div>
                <nav id="nav-menu" class="clearfix">
                    <a class="current" href="https://luckly.work/">首页</a>
                    
                    <a  href="https://luckly.work/categories/" title="分类">分类</a>
                    
                    <a  href="https://luckly.work/tags/" title="标签">标签</a>
                    
                    <a  href="https://luckly.work/archives/" title="归档">归档</a>
                    
                    <a  href="https://luckly.work/about/" title="关于我">关于我</a>
                    
                    <a  href="https://github.com/ITmxs/" title="github">github</a>
                    
                </nav>
            </div>
        </div>
    </div>
</header>

    <div id="body">
        <div class="container">
            <div class="col-group">

                <div class="col-8" id="main">
                    
<div class="res-cons">
    <style type="text/css">
    .post-toc {
        position: fixed;
        width: 200px;
        margin-left: -210px;
        padding: 5px 10px;
        font-family: Athelas, STHeiti, Microsoft Yahei, serif;
        font-size: 12px;
        border: 1px solid rgba(0, 0, 0, .07);
        border-radius: 5px;
        background-color: rgba(255, 255, 255, 0.98);
        background-clip: padding-box;
        -webkit-box-shadow: 1px 1px 2px rgba(0, 0, 0, .125);
        box-shadow: 1px 1px 2px rgba(0, 0, 0, .125);
        word-wrap: break-word;
        white-space: nowrap;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        z-index: 999;
        cursor: pointer;
        max-height: 70%;
        overflow-y: auto;
        overflow-x: hidden;
    }

    .post-toc .post-toc-title {
        width: 100%;
        margin: 0 auto;
        font-size: 20px;
        font-weight: 400;
        text-transform: uppercase;
        text-align: center;
    }

    .post-toc .post-toc-content {
        font-size: 15px;
    }

    .post-toc .post-toc-content>nav>ul {
        margin: 10px 0;
    }

    .post-toc .post-toc-content ul {
        padding-left: 20px;
        list-style: square;
        margin: 0.5em;
        line-height: 1.8em;
    }

    .post-toc .post-toc-content ul ul {
        padding-left: 15px;
        display: none;
    }

    @media print,
    screen and (max-width:1057px) {
        .post-toc {
            display: none;
        }
    }
</style>
<div class="post-toc" style="position: absolute; top: 188px;">
    <h2 class="post-toc-title">文章目录</h2>
    <div class="post-toc-content">
        <nav id="TableOfContents">
  <ul>
    <li><a href="#gorm连接mysql数据库">gorm连接mysql数据库</a>
      <ul>
        <li><a href="#1-配置dsn-data-source-name">1. 配置DSN (Data Source Name)</a></li>
        <li><a href="#2-使用gormopen连接数据库">2. 使用gorm.Open连接数据库</a></li>
        <li><a href="#3-gorm调试模式">3. gorm调试模式</a></li>
      </ul>
    </li>
    <li><a href="#二gorm连接池">二.gorm连接池</a></li>
    <li><a href="#二使用gorm链式操作函数查询数据">二、使用gorm链式操作函数查询数据</a>
      <ul>
        <li><a href="#1query">1.query</a></li>
        <li><a href="#2where">2.where</a></li>
        <li><a href="#3select">3.select</a></li>
        <li><a href="#4order">4.order</a></li>
        <li><a href="#5limit--offset">5.limit &amp; Offset</a></li>
        <li><a href="#6count">6.count</a></li>
        <li><a href="#7分组">7.分组</a></li>
      </ul>
    </li>
    <li><a href="#三直接执行sql语句">三、直接执行sql语句</a></li>
  </ul>

  <ul>
    <li><a href="#二gorm更新记录常用方法">二、gorm更新记录常用方法</a>
      <ul>
        <li><a href="#1-save">1. Save</a></li>
        <li><a href="#2-update">2. Update</a></li>
        <li><a href="#3-updates">3. Updates</a></li>
        <li><a href="#4-更新表达式">4. 更新表达式</a></li>
      </ul>
    </li>
  </ul>

  <ul>
    <li><a href="#1-删除模型数据">1. 删除模型数据</a></li>
    <li><a href="#2-根据where条件删除数据">2. 根据Where条件删除数据</a></li>
  </ul>

  <ul>
    <li><a href="#自动事务">自动事务</a></li>
    <li><a href="#手动事务">手动事务</a></li>
  </ul>

  <ul>
    <li><a href="#外键">外键</a></li>
    <li><a href="#关联外键">关联外键</a></li>
    <li><a href="#属于关联查询例子">属于关联查询例子</a></li>
  </ul>

  <ul>
    <li><a href="#外键-1">外键</a></li>
    <li><a href="#关联外键-1">关联外键</a></li>
    <li><a href="#关联查询例子">关联查询例子</a></li>
  </ul>

  <ul>
    <li><a href="#外键-2">外键</a></li>
    <li><a href="#关联外键-2">关联外键</a></li>
    <li><a href="#一对多关联查询例子">一对多关联查询例子</a></li>
  </ul>

  <ul>
    <li><a href="#预加载例子">预加载例子</a></li>
    <li><a href="#自动预加载">自动预加载</a></li>
    <li><a href="#嵌套预加载">嵌套预加载</a></li>
  </ul>

  <ul>
    <li><a href="#自动建表">自动建表</a></li>
    <li><a href="#schema方法">Schema方法</a>
      <ul>
        <li><a href="#检测表是否存在">检测表是否存在</a></li>
        <li><a href="#建表">建表</a></li>
        <li><a href="#删除表">删除表</a></li>
        <li><a href="#删除字段">删除字段</a></li>
        <li><a href="#添加索引">添加索引</a></li>
      </ul>
    </li>
    <li><a href="#组合索引">组合索引</a></li>
  </ul>

  <ul>
    <li><a href="#错误处理">错误处理</a></li>
    <li><a href="#errrecordnotfound-error">ErrRecordNotFound error</a></li>
  </ul>
</nav>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        var postToc = $(".post-toc");
        if (postToc.length) {
            var leftPos = $("#main").offset().left;
            if(leftPos<220){
                postToc.css({"width":leftPos-10,"margin-left":(0-leftPos)})
            }

            var t = postToc.offset().top - 20,
                a = {
                    start: {
                        position: "absolute",
                        top: t
                    },
                    process: {
                        position: "fixed",
                        top: 20
                    },
                };
            $(window).scroll(function () {
                var e = $(window).scrollTop();
                e < t ? postToc.css(a.start) : postToc.css(a.process)
            })
        }
    })
</script>
    <article class="post">
        <header>
            <h1 class="post-title">Gorm连接数据库</h1>
        </header>
        <date class="post-meta meta-date">
            2021年9月13日
        </date>
        
        <div class="post-meta">
            <span>|</span>
            
            <span class="meta-category"><a href='/categories/go'>go</a></span>
            
        </div>
        
        
        <div class="post-meta">
            <span id="busuanzi_container_page_pv">|<span id="busuanzi_value_page_pv"></span><span>
                    阅读</span></span>
        </div>
        
        
        <div class="post-content">
            <h2 id="gorm连接mysql数据库">gorm连接mysql数据库</h2>
<p>gorm支持多种数据库，这里主要介绍mysql,连接mysql主要有两个步骤:</p>
<ol>
<li>配置DSN (Data Source Name)</li>
<li>使用gorm.Open连接数据库</li>
</ol>
<h3 id="1-配置dsn-data-source-name">1. 配置DSN (Data Source Name)</h3>
<p>gorm库使用dsn作为连接数据库的参数，dsn翻译过来就叫数据源名称，用来描述数据库连接信息。一般都包含数据库连接地址，账号，密码之类的信息。</p>
<p><strong>DSN格式：</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go">[<span style="color:#a6e22e">username</span>[:<span style="color:#a6e22e">password</span>]<span style="color:#960050;background-color:#1e0010">@</span>][<span style="color:#a6e22e">protocol</span>[(<span style="color:#a6e22e">address</span>)]]<span style="color:#f92672">/</span><span style="color:#a6e22e">dbname</span>[<span style="color:#960050;background-color:#1e0010">?</span><span style="color:#a6e22e">param1</span>=<span style="color:#a6e22e">value1</span><span style="color:#f92672">&amp;...&amp;</span><span style="color:#a6e22e">paramN</span>=<span style="color:#a6e22e">valueN</span>]
</code></pre></div><p>mysql连接dsn例子：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">//mysql dsn格式
</span><span style="color:#75715e">//涉及参数:
</span><span style="color:#75715e">//username   数据库账号
</span><span style="color:#75715e">//password   数据库密码
</span><span style="color:#75715e">//host       数据库连接地址，可以是Ip或者域名
</span><span style="color:#75715e">//port       数据库端口
</span><span style="color:#75715e">//Dbname     数据库名
</span><span style="color:#75715e"></span><span style="color:#a6e22e">username</span>:<span style="color:#a6e22e">password</span><span style="color:#960050;background-color:#1e0010">@</span><span style="color:#a6e22e">tcp</span>(<span style="color:#a6e22e">host</span>:<span style="color:#a6e22e">port</span>)<span style="color:#f92672">/</span><span style="color:#a6e22e">Dbname</span><span style="color:#960050;background-color:#1e0010">?</span><span style="color:#a6e22e">charset</span>=<span style="color:#a6e22e">utf8</span><span style="color:#f92672">&amp;</span><span style="color:#a6e22e">parseTime</span>=<span style="color:#a6e22e">True</span><span style="color:#f92672">&amp;</span><span style="color:#a6e22e">loc</span>=<span style="color:#a6e22e">Local</span>

<span style="color:#75715e">//填上参数后的例子
</span><span style="color:#75715e">//username = root
</span><span style="color:#75715e">//password = 123456
</span><span style="color:#75715e">//host     = localhost
</span><span style="color:#75715e">//port     = 3306
</span><span style="color:#75715e">//Dbname   = tizi365
</span><span style="color:#75715e">//后面K/V键值对参数含义为：
</span><span style="color:#75715e">//  charset=utf8 客户端字符集为utf8
</span><span style="color:#75715e">//  parseTime=true 支持把数据库datetime和date类型转换为golang的time.Time类型
</span><span style="color:#75715e">//  loc=Local 使用系统本地时区
</span><span style="color:#75715e"></span><span style="color:#a6e22e">root</span>:<span style="color:#ae81ff">123456</span><span style="color:#960050;background-color:#1e0010">@</span><span style="color:#a6e22e">tcp</span>(<span style="color:#a6e22e">localhost</span>:<span style="color:#ae81ff">3306</span>)<span style="color:#f92672">/</span><span style="color:#a6e22e">tizi365</span><span style="color:#960050;background-color:#1e0010">?</span><span style="color:#a6e22e">charset</span>=<span style="color:#a6e22e">utf8</span><span style="color:#f92672">&amp;</span><span style="color:#a6e22e">parseTime</span>=<span style="color:#a6e22e">True</span><span style="color:#f92672">&amp;</span><span style="color:#a6e22e">loc</span>=<span style="color:#a6e22e">Local</span>

<span style="color:#75715e">//gorm 设置mysql连接超时参数
</span><span style="color:#75715e">//开发的时候经常需要设置数据库连接超时参数，gorm是通过dsn的timeout参数配置
</span><span style="color:#75715e">//例如，设置10秒后连接超时，timeout=10s
</span><span style="color:#75715e">//下面是完成的例子
</span><span style="color:#75715e"></span><span style="color:#a6e22e">root</span>:<span style="color:#ae81ff">123456</span><span style="color:#960050;background-color:#1e0010">@</span><span style="color:#a6e22e">tcp</span>(<span style="color:#a6e22e">localhost</span>:<span style="color:#ae81ff">3306</span>)<span style="color:#f92672">/</span><span style="color:#a6e22e">tizi365</span><span style="color:#960050;background-color:#1e0010">?</span><span style="color:#a6e22e">charset</span>=<span style="color:#a6e22e">utf8</span><span style="color:#f92672">&amp;</span><span style="color:#a6e22e">parseTime</span>=<span style="color:#a6e22e">True</span><span style="color:#f92672">&amp;</span><span style="color:#a6e22e">loc</span>=<span style="color:#a6e22e">Local</span><span style="color:#f92672">&amp;</span><span style="color:#a6e22e">timeout</span>=<span style="color:#ae81ff">10</span><span style="color:#a6e22e">s</span>

<span style="color:#75715e">//设置读写超时时间
</span><span style="color:#75715e">// readTimeout - 读超时时间，0代表不限制
</span><span style="color:#75715e">// writeTimeout - 写超时时间，0代表不限制
</span><span style="color:#75715e"></span><span style="color:#a6e22e">root</span>:<span style="color:#ae81ff">123456</span><span style="color:#960050;background-color:#1e0010">@</span><span style="color:#a6e22e">tcp</span>(<span style="color:#a6e22e">localhost</span>:<span style="color:#ae81ff">3306</span>)<span style="color:#f92672">/</span><span style="color:#a6e22e">tizi365</span><span style="color:#960050;background-color:#1e0010">?</span><span style="color:#a6e22e">charset</span>=<span style="color:#a6e22e">utf8</span><span style="color:#f92672">&amp;</span><span style="color:#a6e22e">parseTime</span>=<span style="color:#a6e22e">True</span><span style="color:#f92672">&amp;</span><span style="color:#a6e22e">loc</span>=<span style="color:#a6e22e">Local</span><span style="color:#f92672">&amp;</span><span style="color:#a6e22e">timeout</span>=<span style="color:#ae81ff">10</span><span style="color:#a6e22e">s</span><span style="color:#f92672">&amp;</span><span style="color:#a6e22e">readTimeout</span>=<span style="color:#ae81ff">30</span><span style="color:#a6e22e">s</span><span style="color:#f92672">&amp;</span><span style="color:#a6e22e">writeTimeout</span>=<span style="color:#ae81ff">60</span><span style="color:#a6e22e">s</span>
</code></pre></div><h3 id="2-使用gormopen连接数据库">2. 使用gorm.Open连接数据库</h3>
<p>有了上面配置的dsn参数，就可以使用gorm连接数据库，下面是连接数据库的例子</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#f92672">package</span> <span style="color:#a6e22e">main</span>

<span style="color:#f92672">import</span> (
  <span style="color:#e6db74">&#34;gorm.io/driver/mysql&#34;</span>
  <span style="color:#e6db74">&#34;gorm.io/gorm&#34;</span>
)

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">main</span>()  {
    <span style="color:#75715e">//配置MySQL连接参数
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">username</span> <span style="color:#f92672">:=</span> <span style="color:#e6db74">&#34;root&#34;</span>  <span style="color:#75715e">//账号
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">password</span> <span style="color:#f92672">:=</span> <span style="color:#e6db74">&#34;123456&#34;</span> <span style="color:#75715e">//密码
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">host</span> <span style="color:#f92672">:=</span> <span style="color:#e6db74">&#34;127.0.0.1&#34;</span> <span style="color:#75715e">//数据库地址，可以是Ip或者域名
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">port</span> <span style="color:#f92672">:=</span> <span style="color:#ae81ff">3306</span> <span style="color:#75715e">//数据库端口
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">Dbname</span> <span style="color:#f92672">:=</span> <span style="color:#e6db74">&#34;tizi365&#34;</span> <span style="color:#75715e">//数据库名
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">timeout</span> <span style="color:#f92672">:=</span> <span style="color:#e6db74">&#34;10s&#34;</span> <span style="color:#75715e">//连接超时，10秒
</span><span style="color:#75715e"></span>	
	<span style="color:#75715e">//拼接下dsn参数, dsn格式可以参考上面的语法，这里使用Sprintf动态拼接dsn参数，因为一般数据库连接参数，我们都是保存在配置文件里面，需要从配置文件加载参数，然后拼接dsn。
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">dsn</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Sprintf</span>(<span style="color:#e6db74">&#34;%s:%s@tcp(%s:%d)/%s?charset=utf8&amp;parseTime=True&amp;loc=Local&amp;timeout=%s&#34;</span>, <span style="color:#a6e22e">username</span>, <span style="color:#a6e22e">password</span>, <span style="color:#a6e22e">host</span>, <span style="color:#a6e22e">port</span>, <span style="color:#a6e22e">Dbname</span>, <span style="color:#a6e22e">timeout</span>)
	<span style="color:#75715e">//连接MYSQL, 获得DB类型实例，用于后面的数据库读写操作。
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">db</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">Open</span>(<span style="color:#a6e22e">mysql</span>.<span style="color:#a6e22e">Open</span>(<span style="color:#a6e22e">dsn</span>), <span style="color:#f92672">&amp;</span><span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">Config</span>{})
	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
		panic(<span style="color:#e6db74">&#34;连接数据库失败, error=&#34;</span> <span style="color:#f92672">+</span> <span style="color:#a6e22e">err</span>.<span style="color:#a6e22e">Error</span>())
	}
	<span style="color:#75715e">//延时关闭数据库连接
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">defer</span> <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Close</span>()
}
</code></pre></div><h3 id="3-gorm调试模式">3. gorm调试模式</h3>
<p>为了方便调试，了解gorm操作到底执行了怎么样的sql语句，<strong>开发的时候</strong>需要打开调试日志，这样gorm会打印出执行的每一条sql语句。</p>
<p>使用Debug函数执行查询即可
例子：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">result</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Debug</span>().<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;username = ?&#34;</span>, <span style="color:#e6db74">&#34;tizi365&#34;</span>).<span style="color:#a6e22e">First</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">u</span>)
</code></pre></div><h2 id="二gorm连接池">二.gorm连接池</h2>
<p>在高并发实践中，为了提高数据库连接的使用率，避免重复建立数据库连接带来的性能消耗，会经常使用数据库连接池技术来维护数据库连接。
gorm自带了数据库连接池使用非常简单只要设置下数据库连接池参数即可。</p>
<p>数据库连接池使用例子：
定义tools包，负责数据库初始化工作</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">//定义一个工具包，用来管理gorm数据库连接池的初始化工作。
</span><span style="color:#75715e"></span><span style="color:#f92672">package</span> <span style="color:#a6e22e">tools</span>

<span style="color:#75715e">//定义全局的db对象，我们执行数据库操作主要通过他实现。
</span><span style="color:#75715e"></span><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">_db</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">DB</span>

<span style="color:#75715e">//包初始化函数，golang特性，每个包初始化的时候会自动执行init函数，这里用来初始化gorm。
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">init</span>() {
    <span style="color:#f92672">...</span><span style="color:#a6e22e">忽略dsn配置</span><span style="color:#960050;background-color:#1e0010">，</span><span style="color:#a6e22e">请参考上面例子</span><span style="color:#f92672">...</span>
    
    <span style="color:#75715e">// 声明err变量，下面不能使用:=赋值运算符，否则_db变量会当成局部变量，导致外部无法访问_db变量
</span><span style="color:#75715e"></span>    <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">err</span> <span style="color:#66d9ef">error</span>
    <span style="color:#a6e22e">_db</span>, <span style="color:#a6e22e">err</span> = <span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">Open</span>(<span style="color:#a6e22e">mysql</span>.<span style="color:#a6e22e">Open</span>(<span style="color:#a6e22e">dsn</span>), <span style="color:#f92672">&amp;</span><span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">Config</span>{})
    <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
		panic(<span style="color:#e6db74">&#34;连接数据库失败, error=&#34;</span> <span style="color:#f92672">+</span> <span style="color:#a6e22e">err</span>.<span style="color:#a6e22e">Error</span>())
	}
     
    <span style="color:#a6e22e">sqlDB</span>, <span style="color:#a6e22e">_</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">DB</span>()

    <span style="color:#75715e">//设置数据库连接池参数
</span><span style="color:#75715e"></span>    <span style="color:#a6e22e">sqlDB</span>.<span style="color:#a6e22e">SetMaxOpenConns</span>(<span style="color:#ae81ff">100</span>)   <span style="color:#75715e">//设置数据库连接池最大连接数
</span><span style="color:#75715e"></span>    <span style="color:#a6e22e">sqlDB</span>.<span style="color:#a6e22e">SetMaxIdleConns</span>(<span style="color:#ae81ff">20</span>)   <span style="color:#75715e">//连接池最大允许的空闲连接数，如果没有sql任务需要执行的连接数大于20，超过的连接会被连接池关闭。
</span><span style="color:#75715e"></span>    <span style="color:#a6e22e">_db</span> = <span style="color:#a6e22e">db</span>
}

<span style="color:#75715e">//获取gorm db对象，其他包需要执行数据库查询的时候，只要通过tools.getDB()获取db对象即可。
</span><span style="color:#75715e">//不用担心协程并发使用同样的db对象会共用同一个连接，db对象在调用他的方法的时候会从数据库连接池中获取新的连接
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">GetDB</span>() <span style="color:#f92672">*</span><span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">DB</span> {
	<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">_db</span>
}
</code></pre></div><p>使用例子：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#f92672">package</span> <span style="color:#a6e22e">main</span>
<span style="color:#75715e">//导入tools包
</span><span style="color:#75715e"></span><span style="color:#f92672">import</span> <span style="color:#a6e22e">tools</span>

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">main</span>() {
   <span style="color:#a6e22e">db</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">tools</span>.<span style="color:#a6e22e">GetDB</span>()
	<span style="color:#75715e">//定义一个用户，并初始化数据
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">u</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">User</span>{
		<span style="color:#a6e22e">Username</span>:   <span style="color:#e6db74">&#34;瘦子&#34;</span>,
		<span style="color:#a6e22e">Password</span>:   <span style="color:#e6db74">&#34;123456&#34;</span>,
		<span style="color:#a6e22e">CreateTime</span>: <span style="color:#a6e22e">time</span>.<span style="color:#a6e22e">Now</span>().<span style="color:#a6e22e">Unix</span>(),
	}

	<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">AutoMigrate</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">u</span>)
	<span style="color:#75715e">//一般项目中我们会类似下面的写法，通过Error对象检测，插入数据有没有成功，如果没有错误那就是数据写入成功了。
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Create</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">u</span>).<span style="color:#a6e22e">Error</span>; <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
		<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;插入失败&#34;</span>, <span style="color:#a6e22e">err</span>)
		<span style="color:#66d9ef">return</span>
	}
}
</code></pre></div><blockquote>
<p>注意：使用连接池技术后，千万不要使用完db后调用db.Close关闭数据库连接，这样会导致整个数据库连接池关闭，导致连接池没有可用的连接。</p>
</blockquote>
<h2 id="二使用gorm链式操作函数查询数据">二、使用gorm链式操作函数查询数据</h2>
<p>gorm查询主要由以下几个部分的函数组成，这些函数可以串起来组合sql语句，使用起来类似编写sql语句的习惯。</p>
<h3 id="1query">1.query</h3>
<p>执行查询的函数，gorm提供下面几个查询函数：</p>
<ul>
<li><strong>Take</strong>
查询一条记录</li>
</ul>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">例子</span><span style="color:#960050;background-color:#1e0010">：</span>

<span style="color:#75715e">//定义接收查询结果的结构体变量
</span><span style="color:#75715e"></span><span style="color:#a6e22e">User</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">User</span>{}

<span style="color:#75715e">//等价于：SELECT * FROM `Users`   LIMIT 1  
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Take</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">User</span>)
</code></pre></div><ul>
<li><strong>First</strong>
查询一条记录，根据主键ID排序(<strong>正序</strong>)，返回第一条记录</li>
</ul>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">例子</span><span style="color:#960050;background-color:#1e0010">：</span>

<span style="color:#75715e">//等价于：SELECT * FROM `Users`   ORDER BY `Users`.`id` ASC LIMIT 1    
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">First</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">User</span>)
</code></pre></div><ul>
<li><strong>Last</strong>
查询一条记录, 根据主键ID排序(<strong>倒序</strong>)，返回第一条记录</li>
</ul>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">//等价于：SELECT * FROM `Users`   ORDER BY `Users`.`id` DESC LIMIT 1   
</span><span style="color:#75715e">//语义上相当于返回最后一条记录
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Last</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">User</span>)
</code></pre></div><ul>
<li><strong>Find</strong>
查询多条记录，Find函数返回的是一个数组</li>
</ul>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">//因为Find返回的是数组，所以定义一个商品数组用来接收结果
</span><span style="color:#75715e"></span><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">Users</span> []<span style="color:#a6e22e">User</span>

<span style="color:#75715e">//等价于：SELECT * FROM `Users`
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">Users</span>)
</code></pre></div><ul>
<li><strong>Pluck</strong>
查询一列值</li>
</ul>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">//商品标题数组
</span><span style="color:#75715e"></span><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">titles</span> []<span style="color:#66d9ef">string</span>

<span style="color:#75715e">//返回所有商品标题
</span><span style="color:#75715e">//等价于：SELECT title FROM `Users`
</span><span style="color:#75715e">//Pluck提取了title字段，保存到titles变量
</span><span style="color:#75715e">//这里Model函数是为了绑定一个模型实例，可以从里面提取表名。
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">User</span>{}).<span style="color:#a6e22e">Pluck</span>(<span style="color:#e6db74">&#34;title&#34;</span>, <span style="color:#f92672">&amp;</span><span style="color:#a6e22e">titles</span>)
</code></pre></div><h4 id="查询错误处理">查询错误处理</h4>
<p>users</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">例子</span><span style="color:#960050;background-color:#1e0010">：</span>
<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Take</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">User</span>).<span style="color:#a6e22e">Error</span>; <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
    <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;查询失败&#34;</span>, <span style="color:#a6e22e">err</span>)
}
</code></pre></div><p><strong>错误特例：</strong></p>
<p>当 First、Last、Take 方法找不到记录时，GORM 会返回 ErrRecordNotFound 错误。</p>
<p>在实际开发中查询不到数据，我们不一定会当成错误处理, gorm库通过下面办法检测Error是不是查询不到数据.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">例子</span>:
<span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Take</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">User</span>).<span style="color:#a6e22e">Error</span>
<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">errors</span>.<span style="color:#a6e22e">Is</span>(<span style="color:#a6e22e">err</span>, <span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">ErrRecordNotFound</span>) {
    <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;查询不到数据&#34;</span>)
} <span style="color:#66d9ef">else</span> <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
<span style="color:#75715e">//如果err不等于record not found错误，又不等于nil，那说明sql执行失败了。
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;查询失败&#34;</span>, <span style="color:#a6e22e">err</span>)
}
</code></pre></div><h3 id="2where">2.where</h3>
<p>上面的例子都没有指定where条件，这里介绍下如何设置where条件，主要通过db.Where函数设置条件.
函数说明：
<em>db.Where(query interface{}, args &hellip;interface{})</em></p>
<p>参数说明:</p>
<table>
<thead>
<tr>
<th style="text-align:left">参数名</th>
<th style="text-align:left">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">query</td>
<td style="text-align:left">sql语句的where子句, where子句中使用问号(?)代替参数值，则表示通过args参数绑定参数</td>
</tr>
<tr>
<td style="text-align:left">args</td>
<td style="text-align:left">where子句绑定的参数，可以绑定多个参数</td>
</tr>
</tbody>
</table>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">例子1</span>:
<span style="color:#75715e">//等价于: SELECT * FROM `Users`  WHERE (id = &#39;10&#39;) LIMIT 1
</span><span style="color:#75715e">//这里问号(?), 在执行的时候会被10替代
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;id = ?&#34;</span>, <span style="color:#ae81ff">10</span>).<span style="color:#a6e22e">Take</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">User</span>)

<span style="color:#75715e">//例子2:
</span><span style="color:#75715e">// in 语句 
</span><span style="color:#75715e">//等价于: SELECT * FROM `Users`  WHERE (id in (&#39;1&#39;,&#39;2&#39;,&#39;5&#39;,&#39;6&#39;)) LIMIT 1 
</span><span style="color:#75715e">//args参数传递的是数组
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;id in (?)&#34;</span>, []<span style="color:#66d9ef">int</span>{<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">2</span>,<span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">6</span>}).<span style="color:#a6e22e">Take</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">User</span>)

<span style="color:#75715e">//例子3:
</span><span style="color:#75715e">//等价于: SELECT * FROM `Users`  WHERE (create_time &gt;= &#39;2018-11-06 00:00:00&#39; and create_time &lt;= &#39;2018-11-06 23:59:59&#39;)
</span><span style="color:#75715e">//这里使用了两个问号(?)占位符，后面传递了两个参数替换两个问号。
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;create_time &gt;= ? and create_time &lt;= ?&#34;</span>, <span style="color:#e6db74">&#34;2018-11-06 00:00:00&#34;</span>, <span style="color:#e6db74">&#34;2018-11-06 23:59:59&#34;</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">Users</span>)

<span style="color:#75715e">//例子4:
</span><span style="color:#75715e">//like语句
</span><span style="color:#75715e">//等价于: SELECT * FROM `Users`  WHERE (title like &#39;%可乐%&#39;)
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;title like ?&#34;</span>, <span style="color:#e6db74">&#34;%可乐%&#34;</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">Users</span>)
</code></pre></div><h3 id="3select">3.select</h3>
<p>设置select子句, 指定返回的字段</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">//例子1:
</span><span style="color:#75715e">//等价于: SELECT id,title FROM `Users`  WHERE `Users`.`id` = &#39;1&#39; AND ((id = &#39;1&#39;)) LIMIT 1  
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Select</span>(<span style="color:#e6db74">&#34;id,title&#34;</span>).<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;id = ?&#34;</span>, <span style="color:#ae81ff">1</span>).<span style="color:#a6e22e">Take</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">User</span>)

<span style="color:#75715e">//这种写法是直接往Select函数传递数组，数组元素代表需要选择的字段名
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Select</span>([]<span style="color:#66d9ef">string</span>{<span style="color:#e6db74">&#34;id&#34;</span>, <span style="color:#e6db74">&#34;title&#34;</span>}).<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;id = ?&#34;</span>, <span style="color:#ae81ff">1</span>).<span style="color:#a6e22e">Take</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">User</span>)


<span style="color:#75715e">//例子2:
</span><span style="color:#75715e">//可以直接书写聚合语句
</span><span style="color:#75715e">//等价于: SELECT count(*) as total FROM `Users`
</span><span style="color:#75715e"></span><span style="color:#a6e22e">total</span> <span style="color:#f92672">:=</span> []<span style="color:#66d9ef">int</span>{}

<span style="color:#75715e">//Model函数，用于指定绑定的模型，这里生成了一个User{}变量。目的是从模型变量里面提取表名，Pluck函数我们没有直接传递绑定表名的结构体变量，gorm库不知道表名是什么，所以这里需要指定表名
</span><span style="color:#75715e">//Pluck函数，主要用于查询一列值
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">User</span>{}).<span style="color:#a6e22e">Select</span>(<span style="color:#e6db74">&#34;count(*) as total&#34;</span>).<span style="color:#a6e22e">Pluck</span>(<span style="color:#e6db74">&#34;total&#34;</span>, <span style="color:#f92672">&amp;</span><span style="color:#a6e22e">total</span>)

<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">total</span>[<span style="color:#ae81ff">0</span>])
</code></pre></div><h3 id="4order">4.order</h3>
<p>设置排序语句，order by子句</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">//例子:
</span><span style="color:#75715e">//等价于: SELECT * FROM `Users`  WHERE (create_time &gt;= &#39;2018-11-06 00:00:00&#39;) ORDER BY create_time desc
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;create_time &gt;= ?&#34;</span>, <span style="color:#e6db74">&#34;2018-11-06 00:00:00&#34;</span>).<span style="color:#a6e22e">Order</span>(<span style="color:#e6db74">&#34;create_time desc&#34;</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">Users</span>)
</code></pre></div><h3 id="5limit--offset">5.limit &amp; Offset</h3>
<p>设置limit和Offset子句，分页的时候常用语句。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">//等价于: SELECT * FROM `Users` ORDER BY create_time desc LIMIT 10 OFFSET 0 
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Order</span>(<span style="color:#e6db74">&#34;create_time desc&#34;</span>).<span style="color:#a6e22e">Limit</span>(<span style="color:#ae81ff">10</span>).<span style="color:#a6e22e">Offset</span>(<span style="color:#ae81ff">0</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">Users</span>)
</code></pre></div><h3 id="6count">6.count</h3>
<p>Count函数，直接返回查询匹配的行数。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">//例子:
</span><span style="color:#75715e"></span><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">total</span> <span style="color:#66d9ef">int64</span> = <span style="color:#ae81ff">0</span>
<span style="color:#75715e">//等价于: SELECT count(*) FROM `Users` 
</span><span style="color:#75715e">//这里也需要通过model设置模型，让gorm可以提取模型对应的表名
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#a6e22e">User</span>{}).<span style="color:#a6e22e">Count</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">total</span>)
<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">total</span>)
</code></pre></div><h3 id="7分组">7.分组</h3>
<p>设置group by子句</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">//例子:
</span><span style="color:#75715e">//统计每个商品分类下面有多少个商品
</span><span style="color:#75715e">//定一个Result结构体类型，用来保存查询结果
</span><span style="color:#75715e"></span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">Result</span> <span style="color:#66d9ef">struct</span> {
    <span style="color:#a6e22e">Type</span>  <span style="color:#66d9ef">int</span>
    <span style="color:#a6e22e">Total</span> <span style="color:#66d9ef">int</span>
}

<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">results</span> []<span style="color:#a6e22e">Result</span>
<span style="color:#75715e">//等价于: SELECT type, count(*) as  total FROM `Users` GROUP BY type HAVING (total &gt; 0)
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#a6e22e">User</span>{}).<span style="color:#a6e22e">Select</span>(<span style="color:#e6db74">&#34;type, count(*) as  total&#34;</span>).<span style="color:#a6e22e">Group</span>(<span style="color:#e6db74">&#34;type&#34;</span>).<span style="color:#a6e22e">Having</span>(<span style="color:#e6db74">&#34;total &gt; 0&#34;</span>).<span style="color:#a6e22e">Scan</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">results</span>)

<span style="color:#75715e">//scan类似Find都是用于执行查询语句，然后把查询结果赋值给结构体变量，区别在于scan不会从传递进来的结构体变量提取表名.
</span><span style="color:#75715e">//这里因为我们重新定义了一个结构体用于保存结果，但是这个结构体并没有绑定Users表，所以这里只能使用scan查询函数。
</span></code></pre></div><blockquote>
<p>提示：Group函数必须搭配Select函数一起使用</p>
</blockquote>
<h2 id="三直接执行sql语句">三、直接执行sql语句</h2>
<p>对于复杂的查询，例如多表连接查询，我们可以直接编写sql语句，然后执行sql语句。
gorm通过db.Raw设置sql语句，通过Scan执行查询。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-gherkin" data-lang="gherkin"><span style="color:#66d9ef">例子:</span><span style="color:#a6e22e">
</span><span style="color:#a6e22e">sql := &#34;SELECT type, count(*) as  total FROM `Users` where create_time &gt; ? GROUP BY type HAVING (total &gt; 0)&#34;
</span><span style="color:#a6e22e">//因为sql语句使用了一个问号(?)作为绑定参数, 所以需要传递一个绑定参数(Raw第二个参数).
</span><span style="color:#a6e22e">//Raw函数支持绑定多个参数
</span><span style="color:#a6e22e">db.Raw(sql, &#34;2018-11-06 00:00:00&#34;).Scan(&amp;results)
</span><span style="color:#a6e22e">fmt.Println(results)
</span></code></pre></div><h1 id="gorm更新数据">GORM更新数据</h1>
<h2 id="二gorm更新记录常用方法">二、gorm更新记录常用方法</h2>
<h3 id="1-save">1. Save</h3>
<p>用于保存模型变量的值。</p>
<blockquote>
<p>提示: 相当于根据主键id，更新所有模型字段值。</p>
</blockquote>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">user</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">User</span>{}
<span style="color:#75715e">//先查询一条记录, 保存在模型变量user
</span><span style="color:#75715e">//等价于: SELECT * FROM `users`  WHERE (id = &#39;2&#39;) LIMIT 1
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;id = ?&#34;</span>, <span style="color:#ae81ff">2</span>).<span style="color:#a6e22e">Take</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)

<span style="color:#75715e">//修改user模型的值
</span><span style="color:#75715e"></span><span style="color:#a6e22e">user</span>.<span style="color:#a6e22e">Price</span> = <span style="color:#ae81ff">100</span>

<span style="color:#75715e">//等价于: UPDATE `users` SET `title` = &#39;可乐&#39;, `type` = &#39;0&#39;, `price` = &#39;100&#39;, `stock` = &#39;26&#39;, `create_time` = &#39;2018-11-06 11:12:04&#39;  WHERE `users`.`id` = &#39;2&#39;
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Save</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
</code></pre></div><h3 id="2-update">2. Update</h3>
<p>更新单个字段值</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">//例子1:
</span><span style="color:#75715e">//更新user模型对应的表记录
</span><span style="color:#75715e">//等价于: UPDATE `users` SET `price` = &#39;25&#39;  WHERE `users`.`id` = &#39;2&#39;
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>).<span style="color:#a6e22e">Update</span>(<span style="color:#e6db74">&#34;price&#34;</span>, <span style="color:#ae81ff">25</span>)
<span style="color:#75715e">//通过user模型的主键id的值作为where条件，更新price字段值。
</span><span style="color:#75715e"></span>

<span style="color:#75715e">//例子2:
</span><span style="color:#75715e">//上面的例子只是更新一条记录，如果我们要更全部记录怎么办？
</span><span style="color:#75715e">//等价于: UPDATE `users` SET `price` = &#39;25&#39;
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">User</span>{}).<span style="color:#a6e22e">Update</span>(<span style="color:#e6db74">&#34;price&#34;</span>, <span style="color:#ae81ff">25</span>)
<span style="color:#75715e">//注意这里的Model参数，使用的是User{}，新生成一个空白的模型变量，没有绑定任何记录。
</span><span style="color:#75715e">//因为User{}的id为空，gorm库就不会以id作为条件，where语句就是空的
</span><span style="color:#75715e"></span>
<span style="color:#75715e">//例子3:
</span><span style="color:#75715e">//根据自定义条件更新记录，而不是根据主键id
</span><span style="color:#75715e">//等价于: UPDATE `users` SET `price` = &#39;25&#39;  WHERE (create_time &gt; &#39;2018-11-06 20:00:00&#39;) 
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">User</span>{}).<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;create_time &gt; ?&#34;</span>, <span style="color:#e6db74">&#34;2018-11-06 20:00:00&#34;</span>).<span style="color:#a6e22e">Update</span>(<span style="color:#e6db74">&#34;price&#34;</span>, <span style="color:#ae81ff">25</span>)
</code></pre></div><h3 id="3-updates">3. Updates</h3>
<p>更新多个字段值</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">//例子1：
</span><span style="color:#75715e">//通过结构体变量设置更新字段
</span><span style="color:#75715e"></span><span style="color:#a6e22e">updataUser</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">User</span>{
		<span style="color:#a6e22e">Price</span>:<span style="color:#ae81ff">120</span>,
		<span style="color:#a6e22e">Title</span>:<span style="color:#e6db74">&#34;柠檬雪碧&#34;</span>,
	}

<span style="color:#75715e">//根据user模型更新数据库记录
</span><span style="color:#75715e">//等价于: UPDATE `users` SET `price` = &#39;120&#39;, `title` = &#39;柠檬雪碧&#39;  WHERE `users`.`id` = &#39;2&#39;
</span><span style="color:#75715e">//Updates会忽略掉updataUser结构体变量的零值字段, 所以生成的sql语句只有price和title字段。
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>).<span style="color:#a6e22e">Updates</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">updataUser</span>)

<span style="color:#75715e">//例子2:
</span><span style="color:#75715e">//根据自定义条件更新记录，而不是根据模型id
</span><span style="color:#75715e"></span><span style="color:#a6e22e">updataUser</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">User</span>{
		<span style="color:#a6e22e">Stock</span>:<span style="color:#ae81ff">120</span>,
		<span style="color:#a6e22e">Title</span>:<span style="color:#e6db74">&#34;柠檬雪碧&#34;</span>,
	}
	
<span style="color:#75715e">//设置Where条件，Model参数绑定一个空的模型变量
</span><span style="color:#75715e">//等价于: UPDATE `users` SET `stock` = &#39;120&#39;, `title` = &#39;柠檬雪碧&#39;  WHERE (price &gt; &#39;10&#39;) 
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">User</span>{}).<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;price &gt; ?&#34;</span>, <span style="color:#ae81ff">10</span>).<span style="color:#a6e22e">Updates</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">updataUser</span>)

<span style="color:#75715e">//例子3:
</span><span style="color:#75715e">//如果想更新所有字段值，包括零值，就是不想忽略掉空值字段怎么办？
</span><span style="color:#75715e">//使用map类型，替代上面的结构体变量
</span><span style="color:#75715e"></span>
<span style="color:#75715e">//定义map类型，key为字符串，value为interface{}类型，方便保存任意值
</span><span style="color:#75715e"></span><span style="color:#a6e22e">data</span> <span style="color:#f92672">:=</span> make(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">interface</span>{})
<span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;stock&#34;</span>] = <span style="color:#ae81ff">0</span> <span style="color:#75715e">//零值字段
</span><span style="color:#75715e"></span><span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;price&#34;</span>] = <span style="color:#ae81ff">35</span>

<span style="color:#75715e">//等价于: UPDATE `users` SET `price` = &#39;35&#39;, `stock` = &#39;0&#39;  WHERE (id = &#39;2&#39;)
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">User</span>{}).<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;id = ?&#34;</span>, <span style="color:#ae81ff">2</span>).<span style="color:#a6e22e">Updates</span>(<span style="color:#a6e22e">data</span>)
</code></pre></div><blockquote>
<p>提示： 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, &ldquo;&rdquo;, false这些值会被忽略掉，不会更新。如果想更新零值，可以使用map类型替代结构体。</p>
</blockquote>
<h3 id="4-更新表达式">4. 更新表达式</h3>
<p><em>UPDATE <code>users</code> SET <code>stock</code> = <code>stock</code> + 1 WHERE id = &lsquo;2&rsquo;</em>
这样的带计算表达式的更新语句gorm怎么写？</p>
<p>gorm提供了Expr函数用于设置表达式</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">//等价于: UPDATE `users` SET `stock` = stock + 1  WHERE `users`.`id` = &#39;2&#39;
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>).<span style="color:#a6e22e">Update</span>(<span style="color:#e6db74">&#34;stock&#34;</span>, <span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">Expr</span>(<span style="color:#e6db74">&#34;stock + 1&#34;</span>))
</code></pre></div><h1 id="gorm删除数据">GORM删除数据</h1>
<h2 id="1-删除模型数据">1. 删除模型数据</h2>
<p>删除模型数据一般用于删除之前查询出来的模型变量绑定的记录。
<em>用法：db.Delete(模型变量)</em></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">//例子：
</span><span style="color:#75715e"></span><span style="color:#a6e22e">user</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">user</span>{}
<span style="color:#75715e">//先查询一条记录, 保存在模型变量user
</span><span style="color:#75715e">//等价于: SELECT * FROM `users`  WHERE (id = &#39;2&#39;) LIMIT 1
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;id = ?&#34;</span>, <span style="color:#ae81ff">2</span>).<span style="color:#a6e22e">Take</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)

<span style="color:#75715e">//删除user对应的记录，通过主键Id标识记录
</span><span style="color:#75715e">//等价于： DELETE from `users` where id=2;
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Delete</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
</code></pre></div><h2 id="2-根据where条件删除数据">2. 根据Where条件删除数据</h2>
<p><em>用法：db.Where(条件表达式).Delete(空模型变量指针)</em></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">//等价于：DELETE from `users` where (`type` = 5);
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;type = ?&#34;</span>, <span style="color:#ae81ff">5</span>).<span style="color:#a6e22e">Delete</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>{})
</code></pre></div><blockquote>
<p>提示：这里Delete函数需要传递一个空的模型变量指针，主要用于获取模型变量绑定的表名。 不能传递一个非空的模型变量，否则就变成删除指定的模型数据，自动在where语句加上类似id = 2这样的主键约束条件。</p>
</blockquote>
<h1 id="gorm事务处理">GORM事务处理</h1>
<h2 id="自动事务">自动事务</h2>
<p>通过db.Transaction函数实现事务，如果闭包函数返回错误，则回滚事务。</p>
<pre><code>db.Transaction(func(tx *gorm.DB) error {
  // 在事务中执行一些 db 操作（从这里开始，您应该使用 'tx' 而不是 'db'）
  if err := tx.Create(&amp;Animal{Name: &quot;Giraffe&quot;}).Error; err != nil {
    // 返回任何错误都会回滚事务
    return err
  }

  if err := tx.Create(&amp;Animal{Name: &quot;Lion&quot;}).Error; err != nil {
    return err
  }

  // 返回 nil 提交事务
  return nil
})
</code></pre><h2 id="手动事务">手动事务</h2>
<p>在开发中经常需要数据库事务来保证多个数据库写操作的原子性。例如电商系统中的扣减库存和保存订单。
gorm事务用法：</p>
<pre><code>// 开启事务
tx := db.Begin()

//在事务中执行数据库操作，使用的是tx变量，不是db。

//库存减一
//等价于: UPDATE `users` SET `stock` = stock - 1  WHERE `users`.`id` = '2' and stock &gt; 0
//RowsAffected用于返回sql执行后影响的行数
rowsAffected := tx.Model(&amp;user).Where(&quot;stock &gt; 0&quot;).Update(&quot;stock&quot;, gorm.Expr(&quot;stock - 1&quot;)).RowsAffected
if rowsAffected == 0 {
    //如果更新库存操作，返回影响行数为0，说明没有库存了，结束下单流程
    //这里回滚作用不大，因为前面没成功执行什么数据库更新操作，也没什么数据需要回滚。
    //这里就是举个例子，事务中可以执行多个sql语句，错误了可以回滚事务
    tx.Rollback()
    return
}
err := tx.Create(保存订单).Error

//保存订单失败，则回滚事务
if err != nil {
    tx.Rollback()
} else {
    tx.Commit()
}
</code></pre><h1 id="gorm-关联查询-属于">GORM 关联查询-属于</h1>
<p>GORM的关联查询（又叫连表查询）中的<strong>属于</strong>关系是<strong>一对一</strong>关联关系的一种，通常用于描述一个Model属于另外一个Model。</p>
<p><strong>例子</strong></p>
<p>存在一个users表和profiles表：</p>
<ul>
<li>users - 用户表</li>
<li>profiles - 用户个性化信息表</li>
</ul>
<p>他们之间存在一对一关系，每一个用户都有自己的个性化数据，那么可以说每一条profiles记录都<strong>属于</strong>某个用户。</p>
<pre><code>// 用户表 - 下面使用go struct表示表结构
type User struct {
  // 继承gorm的基础Model,里面默认定义了ID、CreatedAt、UpdatedAt、DeletedAt 4个字段
  gorm.Model
  Name string
}

// 个性化信息表
type Profile struct {
  gorm.Model
  UserID uint // 外键
  // 定义user属性关联users表，默认情况使用 类型名 + ID 组成外键名，在这里UserID属性就是外键
  User   User
  Name   string
}
</code></pre><h2 id="外键">外键</h2>
<p>在关联查询中必须包含外键，默认gorm使用（关联属性类型 + 主键）组成外键名，如上面的例子User + ID 组成UserID，UserID就作为Profile的外键。</p>
<p>也可以通过下面方式修改外键</p>
<pre><code>type Profile struct {
  gorm.Model
  Name      string
  User      User `gorm:&quot;foreignkey:UserRefer&quot;` //使用 UserRefer 作为外键
  UserRefer uint // 外键
}
</code></pre><h2 id="关联外键">关联外键</h2>
<p>在连表操作中，除了外键，还需要一个关联外键组成一对才能完成连表，例如上面的例子，Profile中UserID属性作为外键，它和User中的ID进行关联，这里User的ID就是关联外键。</p>
<p>默认GORM使用主键作为关联外键，所以上面的User使用ID作为关联外键。</p>
<p>也可以自定义关联外键</p>
<pre><code>type User struct {
  gorm.Model
  Refer string // 关联外键
  Name string
}

type Profile struct {
  gorm.Model
  Name      string
  User      User `gorm:&quot;references:Refer&quot;` // 使用 Refer 作为关联外键
  UserRefer string
}
</code></pre><h2 id="属于关联查询例子">属于关联查询例子</h2>
<pre><code>profile := Profile{}
// 查询用户个性数据
//自动生成sql： SELECT * FROM `profiles` WHERE id = 1 AND `profiles`.`deleted_at` IS NULL LIMIT 1
db.Where(&quot;id = ?&quot;, 1).Take(&amp;profile)
fmt.Println(profile)

user := User{}
// 通过Profile关联查询user数据, 查询结果保存到user变量
db.Model(&amp;profile).Association(&quot;User&quot;).Find(&amp;user)
fmt.Println(user)
// 自动生成sql: SELECT * FROM `users` WHERE `users`.`id` = 1 // 1 就是user的 ID，已经自动关联
</code></pre><h1 id="gorm-关联查询---一对一关系has-one">GORM 关联查询 - 一对一关系（has one）</h1>
<p>GORM的关联查询（又叫连表查询）中的Has One关系是<strong>一对一</strong>关联关系的一种，通常用于描述一个Model拥有另外一个Model。</p>
<blockquote>
<p>提示：Has one很像属于（belongs to）关系，都是一对一关系，区别是Has One关系和属于关系，持有关联Model属性的关系是相反的，例如：A 关联 B，Has One关系通常是A 结构体持有B属性， belongs to关系则是B结构体持有A</p>
</blockquote>
<p><strong>例子</strong></p>
<p>每一个用户都有一张信用卡，下面以Go Struct表示表结构</p>
<pre><code>// 信用卡
type CreditCard struct {
  // 继承gorm的基础Model,里面默认定义了ID、CreatedAt、UpdatedAt、DeletedAt 4个字段
  gorm.Model
  Number   string
  UserID   uint // 外键
}

// 用户
type User struct {
  gorm.Model
  CreditCard   CreditCard // 持有信用卡属性（关联信用卡）
}
</code></pre><h2 id="外键-1">外键</h2>
<p>关联查询必须包含外键，默认情况下Has One关系的外键由持有关联属性的<strong>类型名 + 主键</strong> 组成外键名，如上例，User关联CreditCard的外键就是User + ID = UserID。</p>
<p>通过下面方式可以自定义外键</p>
<pre><code>type CreditCard struct {
  gorm.Model
  Number   string
  UserName string // 外键
}

type User struct {
  gorm.Model
  // 通过标签将外键定义为：UserName
  CreditCard CreditCard `gorm:&quot;foreignkey:UserName&quot;`
}
</code></pre><h2 id="关联外键-1">关联外键</h2>
<p>默认情况下，保存User的时候，会自动将User的主键保存到外键UserID中，关联查询的时候，也会使用外键和关联外键进行关联进行查询，这里User的ID就是关联外键。</p>
<p>自定义关联外键的例子</p>
<pre><code>type CreditCard struct {
  gorm.Model
  Number string
  UID    string
}

type User struct {
  gorm.Model
  Name       `sql:&quot;index&quot;` // 关联外键
  // 自定义关联外键为：name
  CreditCard CreditCard `gorm:&quot;foreignkey:uid;references:name&quot;`
}
</code></pre><h2 id="关联查询例子">关联查询例子</h2>
<pre><code>user := User{}
// 查询用户数据
//自动生成sql： SELECT * FROM `users`  WHERE (username = 'tizi365') LIMIT 1
db.Where(&quot;username = ?&quot;, &quot;tizi365&quot;).First(&amp;user)
fmt.Println(user)

var card CreditCard
////自动生成SQL： SELECT * FROM credit_cards WHERE user_id = 123; // 123 自动从user的ID读取
// 关联查询的结果会填充到card变量
db.Model(&amp;user).Association(&quot;CreditCard&quot;).Find(&amp;card)
</code></pre><h1 id="gorm-关联查询---一对多关系has-many">GORM 关联查询 - 一对多关系（Has Many）</h1>
<p>GORM的关联查询（又叫连表查询）中的Has Many关系是<strong>一对</strong>多关联关系，通常用于描述一个Model拥有多个Model。</p>
<p><strong>例子</strong></p>
<p>一个<strong>用户</strong>拥有多张<strong>信用卡</strong>，下面以Go Struct表示表结构</p>
<pre><code>// 用户
type User struct {
  // 继承gorm的基础Model,里面默认定义了ID、CreatedAt、UpdatedAt、DeletedAt 4个字段
  gorm.Model
  CreditCards []CreditCard // 一对多关联属性，表示多张信用卡
}

// 信用卡
type CreditCard struct {
  gorm.Model
  Number   string // 卡号
  UserID  uint // 默认外键， 用户Id
}
</code></pre><h2 id="外键-2">外键</h2>
<p>默认情况下，GORM使用持有关联属性的 <strong>类型名 + 主键ID</strong> 作为外键名。</p>
<p>如上例，User使用User + ID = UserID 作为外键名。</p>
<p>自定义外键</p>
<pre><code>type User struct {
  gorm.Model
  // 通过标签，将外键定义为：UserRefer
  CreditCards []CreditCard `gorm:&quot;foreignkey:UserRefer&quot;`
}

type CreditCard struct {
  gorm.Model
  Number    string
  UserRefer uint // 新定义的外键名
}
</code></pre><h2 id="关联外键-2">关联外键</h2>
<p>外键和关联外键都是成对出现的，默认情况GORM使用主键ID，作为关联外键。</p>
<p>主键ID，默认为ID，如上面的例子，使用User的ID作为关联外键</p>
<p>自定义关联外键</p>
<pre><code>type User struct {
  gorm.Model
  MemberNumber string // 关联外键字段
  // 使用references定义关联外键名
  CreditCards  []CreditCard `gorm:&quot;foreignkey:UserMemberNumber;references:MemberNumber&quot;`
}

type CreditCard struct {
  gorm.Model
  Number           string
  UserMemberNumber string // 外键字段
}
</code></pre><h2 id="一对多关联查询例子">一对多关联查询例子</h2>
<pre><code>user := User{}
// 查询用户数据
//自动生成sql： SELECT * FROM `users`  WHERE (username = 'tizi365') LIMIT 1
db.Where(&quot;username = ?&quot;, &quot;tizi365&quot;).First(&amp;user)
fmt.Println(user)

//自动生成SQL： SELECT * FROM emails WHERE user_id = 111; // 111 是user的主键ID值
// 关联查询的结果，保存到user.CreditCard属性
db.Model(&amp;user).Association(&quot;CreditCard&quot;).Find(&amp;user.CreditCard)
</code></pre><h1 id="gorm-关联查询预加载-preloading">GORM 关联查询预加载 Preloading</h1>
<p>默认情况下GORM因为性能问题，不会自动加载关联属性的值，gorm通过Preload函数支持预加载（Eager loading）关联数据，下面介绍预加载关联数据的方法。</p>
<h2 id="预加载例子">预加载例子</h2>
<pre><code>// 用户表
type User struct {
  gorm.Model
  Username string
  Orders []Orders // 关联订单，一对多关联关系
}
// 订单表
type Orders struct {
  gorm.Model
  UserID uint // 外键字段 
  Price float64
}

// 预加载Orders字段值，Orders字段是User的关联字段
db.Preload(&quot;Orders&quot;).Find(&amp;users)
// 下面是自动生成的SQL，自动完成关联查询
//// SELECT * FROM users;
//// SELECT * FROM orders WHERE user_id IN (1,2,3,4);


// Preload第2，3个参数支持设置SQL语句条件和绑定参数
db.Preload(&quot;Orders&quot;, &quot;state NOT IN (?)&quot;, &quot;cancelled&quot;).Find(&amp;users)
// 自动生成的SQL如下
//// SELECT * FROM users;
//// SELECT * FROM orders WHERE user_id IN (1,2,3,4) AND state NOT IN ('cancelled');

// 通过组合Where函数一起设置SQL条件
db.Where(&quot;state = ?&quot;, &quot;active&quot;).Preload(&quot;Orders&quot;, &quot;state NOT IN (?)&quot;, &quot;cancelled&quot;).Find(&amp;users)
// 自动生成的SQL如下
//// SELECT * FROM users WHERE state = 'active';
//// SELECT * FROM orders WHERE user_id IN (1,2) AND state NOT IN ('cancelled');

// 预加载Orders、Profile、Role多个关联属性
// ps: 预加载字段，必须是User的属性
db.Preload(&quot;Orders&quot;).Preload(&quot;Profile&quot;).Preload(&quot;Role&quot;).Find(&amp;users)
//// SELECT * FROM users;
//// SELECT * FROM orders WHERE user_id IN (1,2,3,4); // has many
//// SELECT * FROM profiles WHERE user_id IN (1,2,3,4); // has one
//// SELECT * FROM roles WHERE id IN (4,5,6); // belongs to
</code></pre><h2 id="自动预加载">自动预加载</h2>
<pre><code>type User struct {
  gorm.Model
  Name       string
  CompanyID  uint
  Company    Company `gorm:&quot;PRELOAD:false&quot;` // 通过标签属性关闭预加载
  Role       Role                           // 默认开启预加载特性
}
// 通过Set设置gorm:auto_preload属性，开启自动预加载，查询的时候才会自动完成关联查询
db.Set(&quot;gorm:auto_preload&quot;, true).Find(&amp;users)
</code></pre><h2 id="嵌套预加载">嵌套预加载</h2>
<pre><code>// 预加载User.Orders.OrderItems属性值，使用点连接嵌套属性即可
db.Preload(&quot;Orders.OrderItems&quot;).Find(&amp;users)
db.Preload(&quot;Orders&quot;, &quot;state = ?&quot;, &quot;paid&quot;).Preload(&quot;Orders.OrderItems&quot;).Find(&amp;users)
</code></pre><h1 id="gorm-自动建表migration特性">GORM 自动建表（Migration特性）</h1>
<p>GORM支持Migration特性，支持根据Go Struct结构自动生成对应的表结构。</p>
<blockquote>
<p>注意：GORM 的AutoMigrate函数，仅支持建表，不支持修改字段和删除字段，避免意外导致丢失数据。</p>
</blockquote>
<h2 id="自动建表">自动建表</h2>
<p>通过AutoMigrate函数可以快速建表，如果表已经存在不会重复创建。</p>
<pre><code>// 根据User结构体，自动创建表结构.
db.AutoMigrate(&amp;User{})

// 一次创建User、Product、Order三个结构体对应的表结构
db.AutoMigrate(&amp;User{}, &amp;Product{}, &amp;Order{})

// 可以通过Set设置附加参数，下面设置表的存储引擎为InnoDB
db.Set(&quot;gorm:table_options&quot;, &quot;ENGINE=InnoDB&quot;).AutoMigrate(&amp;User{})
</code></pre><h2 id="schema方法">Schema方法</h2>
<h3 id="检测表是否存在">检测表是否存在</h3>
<pre><code>// 检测User结构体对应的表是否存在
db.Migrator().HasTable(&amp;User{})

// 检测表名users是否存在
db.Migrator().HasTable(&quot;users&quot;)
</code></pre><h3 id="建表">建表</h3>
<pre><code>// 根据User结构体建表
db.Migrator().CreateTable(&amp;User{})
</code></pre><h3 id="删除表">删除表</h3>
<pre><code>// 删除User结构体对应的表
db.Migrator().DropTable(&amp;User{})

// 删除表名为users的表
db.Migrator().DropTable(&quot;users&quot;)
</code></pre><h3 id="删除字段">删除字段</h3>
<pre><code>// 删除User结构体对应表中的description字段
db.Migrator().DropColumn(&amp;User{}, &quot;Name&quot;)
</code></pre><h3 id="添加索引">添加索引</h3>
<pre><code>type User struct {
  gorm.Model
  Name string `gorm:&quot;size:255;index:idx_name,unique&quot;`
}

// 为 Name 字段创建索引
db.Migrator().CreateIndex(&amp;User{}, &quot;Name&quot;)
db.Migrator().CreateIndex(&amp;User{}, &quot;idx_name&quot;)

// 为 Name 字段删除索引
db.Migrator().DropIndex(&amp;User{}, &quot;Name&quot;)
db.Migrator().DropIndex(&amp;User{}, &quot;idx_name&quot;)

// 检查索引是否存在
db.Migrator().HasIndex(&amp;User{}, &quot;Name&quot;)
db.Migrator().HasIndex(&amp;User{}, &quot;idx_name&quot;)

type User struct {
  gorm.Model
  Name  string `gorm:&quot;size:255;index:idx_name,unique&quot;`
  Name2 string `gorm:&quot;size:255;index:idx_name_2,unique&quot;`
}
// 修改索引名
db.Migrator().RenameIndex(&amp;User{}, &quot;Name&quot;, &quot;Name2&quot;)
db.Migrator().RenameIndex(&amp;User{}, &quot;idx_name&quot;, &quot;idx_name_2&quot;)
</code></pre><h2 id="组合索引">组合索引</h2>
<p>两个字段使用同一个索引名，Migration将创建复合索引，例如：</p>
<pre><code>type User struct {
    Name   string `gorm:&quot;index:idx_member&quot;`
    Number string `gorm:&quot;index:idx_member&quot;`
}
</code></pre><h1 id="gorm-错误处理">GORM 错误处理</h1>
<p>下面介绍GORM关于错误的处理方式</p>
<h2 id="错误处理">错误处理</h2>
<p>如果在执行SQL查询的时候，出现错误，GORM 会将错误信息保存到 *gorm.DB 的Error字段，我们只要检测Error字段就可以知道是否存在错误。</p>
<pre><code>if err := db.Where(&quot;name = ?&quot;, &quot;tizi365&quot;).First(&amp;user).Error; err != nil {
  // 错误处理
}
</code></pre><p>或者</p>
<pre><code>if result := db.Where(&quot;name = ?&quot;, &quot;jinzhu&quot;).First(&amp;user); result.Error != nil {
  // 错误处理
}
</code></pre><h2 id="errrecordnotfound-error">ErrRecordNotFound error</h2>
<p>当 First、Last、Take 方法找不到记录时，GORM 会返回 ErrRecordNotFound 错误。如果发生了多个错误，你可以通过 errors.Is 判断错误是否为 ErrRecordNotFound，例如：</p>
<pre><code>// 检查错误是否为 RecordNotFound
err := db.First(&amp;user, 100).Error
errors.Is(err, gorm.ErrRecordNotFound)
</code></pre>
        </div>

        
<div class="post-archive">
    <ul class="post-copyright">
        <li><strong>原文作者：</strong><a rel="author" href="https://luckly.work/">luckly</a></li>
        <li style="word-break:break-all"><strong>原文链接：</strong><a href="https://luckly.work/post/go_basic/gorm%E8%BF%9E%E6%8E%A5%E6%95%B0%E6%8D%AE%E5%BA%93/">https://luckly.work/post/go_basic/gorm%E8%BF%9E%E6%8E%A5%E6%95%B0%E6%8D%AE%E5%BA%93/</a></li>
        <li><strong>版权声明：</strong>本作品采用<a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/">知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议</a>进行许可，非商业转载请注明出处（作者，原文链接），商业转载请联系作者获得授权。</li>
    </ul>
</div>
<br/>



        

<div class="post-archive">
    <h2>See Also</h2>
    <ul class="listing">
        
        <li><a href="/post/go_basic/func/">Func</a></li>
        
        <li><a href="/post/go_basic/go%E5%AD%A6%E4%B9%A0%E8%B5%84%E6%96%99%E5%8F%8A%E8%B7%AF%E7%BA%BF%E6%8E%A8%E8%8D%90/">Go学习资料及路线推荐</a></li>
        
        <li><a href="/post/go_basic/go_gin%E8%BF%90%E8%A1%8C%E5%A4%9A%E4%B8%AA%E6%9C%8D%E5%8A%A1/">Go_gin运行多个服务</a></li>
        
        <li><a href="/post/go_basic/go%E6%89%93%E5%8D%B0%E4%B8%80%E4%B8%AA%E6%96%87%E4%BB%B6%E4%BF%A1%E6%81%AF/">Go打印一个文件信息</a></li>
        
        <li><a href="/post/go_basic/go%E6%95%B0%E5%AD%97%E7%9A%84%E6%8C%87%E9%92%88/">Go数字的指针</a></li>
        
    </ul>
</div>


        <div class="post-meta meta-tags">
            
            <ul class="clearfix">
                
                <li><a href='/tags/go'>go</a></li>
                
            </ul>
            
        </div>
    </article>
    
    

    
    
    <div class="post bg-white">
      <script src="https://utteranc.es/client.js"
            repo= "https://github.com/ITmxs/repo"
            issue-term="pathname"
            theme="github-light"
            crossorigin="anonymous"
            async>
      </script>
    </div>
    
</div>

                    <footer id="footer">
    <div>
        &copy; 2021 <a href="https://luckly.work/">早起的年轻人 By luckly</a>
        
        | <a rel="nofollow" target="_blank" href="http://beian.miit.gov.cn/">粤ICP备2021号-1</a>
        
    </div>
    <br />
    <div>
        <div class="github-badge">
            <a href="https://juejin.cn/user/3843548384077192" target="_black" rel="nofollow"><span class="badge-subject">Powered by</span><span class="badge-value bg-blue">掘金</span></a>
        </div>
        <div class="github-badge">
            <a href="https://space.bilibili.com/480883651" target="_black"><span class="badge-subject">Design by</span><span class="badge-value bg-brightgreen">早起的年轻人</span></a>
        </div>
        <div class="github-badge">
            <a href="https://cloud.tencent.com/developer/user/6702670" target="_black"><span class="badge-subject">Theme</span><span class="badge-value bg-yellowgreen">云社区</span></a>
        </div>
    </div>
</footer>


    
    <script type="text/javascript">
        window.MathJax = {
            tex2jax: {
                inlineMath: [['$', '$']],
                processEscapes: true
                }
            };
    </script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML' async></script><script src="https://cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js"></script>

<a id="rocket" href="#top"></a>
<script type="text/javascript" src='/js/totop.js?v=0.0.0' async=""></script>



    <script type="text/javascript" src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script>




    <script src='/js/douban.js'></script>
    <script src="/js/copy-to-clipboard.js"></script>

                </div>

                <div id="secondary">
    <section class="widget">
        <form id="search" action='https://luckly.work/search/' method="get" accept-charset="utf-8" target="_blank" _lpchecked="1">
      
      <input type="text" name="q" maxlength="20" placeholder="Search">
      <input type="hidden" name="sitesearch" value="https://luckly.work/">
      <button type="submit" class="submit icon-search"></button>
</form>
    </section>
    
    <section class="widget">
        <h3 class="widget-title">最近文章</h3>
<ul class="widget-list">
    
    <li>
        <a href="https://luckly.work/post/nginx/nginx%E6%96%87%E4%BB%B6%E5%86%85%E5%AE%B9/" title="Nginx文件内容">Nginx文件内容</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/nginx/nginx%E6%9E%81%E7%AE%80%E6%95%99%E7%A8%8B/" title="Nginx极简教程">Nginx极简教程</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/nginx/%E5%8D%81%E5%88%86%E9%92%9F%E5%85%A5%E9%97%A8nginx/" title="十分钟入门Nginx">十分钟入门Nginx</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/go/Goland%E8%BF%9C%E7%A8%8B%E5%BC%80%E5%8F%91%E9%85%8D%E7%BD%AE/" title="Goland远程开发配置">Goland远程开发配置</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/%E8%AF%BB%E4%B9%A6/%E5%9F%9F%E5%90%8D%E8%A7%A3%E6%9E%90/" title="域名解析">域名解析</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/git/%E6%8F%90%E4%BA%A4%E8%BF%87%E7%A8%8B%E7%9C%81%E7%95%A5%E6%9F%90%E4%BA%9B%E6%96%87%E4%BB%B6/" title="提交过程省略某些文件">提交过程省略某些文件</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/flutter_tips/Flutter_DropdownButton%E7%A4%BA%E4%BE%8B/" title="Flutter_DropdownButton示例">Flutter_DropdownButton示例</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/flutter_tips/Flutter_ExpansionPanelList%E5%92%8CExpansionPanelList.radio%E7%A4%BA%E4%BE%8B/" title="Flutter_ExpansionPanelList和ExpansionPanelList">Flutter_ExpansionPanelList和ExpansionPanelList</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/flutter_tips/Flutter%E5%BE%AE%E4%BF%A1%E5%88%86%E4%BA%AB%E9%93%BE%E6%8E%A5%E8%B7%B3%E5%9B%9EApp%E6%8C%87%E5%AE%9A%E9%A1%B5%E9%9D%A2/" title="Flutter微信分享链接跳回App指定页面">Flutter微信分享链接跳回App指定页面</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F/%E5%A4%96%E5%8C%85%E5%8F%AF%E8%83%BD%E7%9A%84%E9%97%AE%E9%A2%98/" title="外包可能的问题">外包可能的问题</a>
    </li>
    
</ul>
    </section>

    

    <section class="widget">
        <h3 class="widget-title"><a href='/categories/'>分类</a></h3>
<ul class="widget-list">
    
    <li><a href="https://luckly.work/categories/Flutter/">Flutter (326)</a></li>
    
    <li><a href="https://luckly.work/categories/IT/">IT (2)</a></li>
    
    <li><a href="https://luckly.work/categories/Kotlin/">Kotlin (2)</a></li>
    
    <li><a href="https://luckly.work/categories/Mysql/">Mysql (1)</a></li>
    
    <li><a href="https://luckly.work/categories/nginx/">nginx (1)</a></li>
    
    <li><a href="https://luckly.work/categories/Vue/">Vue (6)</a></li>
    
    <li><a href="https://luckly.work/categories/YouTube%E8%A7%86%E9%A2%91%E4%B8%8B%E8%BD%BD/">YouTube视频下载 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/android/">android (6)</a></li>
    
    <li><a href="https://luckly.work/categories/dart/">dart (96)</a></li>
    
    <li><a href="https://luckly.work/categories/Flutter/">Flutter (28)</a></li>
    
    <li><a href="https://luckly.work/categories/gin/">gin (25)</a></li>
    
    <li><a href="https://luckly.work/categories/git/">git (4)</a></li>
    
    <li><a href="https://luckly.work/categories/Go/">Go (102)</a></li>
    
    <li><a href="https://luckly.work/categories/gorm/">gorm (4)</a></li>
    
    <li><a href="https://luckly.work/categories/grpc/">grpc (1)</a></li>
    
    <li><a href="https://luckly.work/categories/html/">html (3)</a></li>
    
    <li><a href="https://luckly.work/categories/ios/">ios (1)</a></li>
    
    <li><a href="https://luckly.work/categories/linux/">linux (1)</a></li>
    
    <li><a href="https://luckly.work/categories/nginx/">nginx (6)</a></li>
    
    <li><a href="https://luckly.work/categories/python/">python (35)</a></li>
    
    <li><a href="https://luckly.work/categories/read/">读书笔记 (6)</a></li>
    
    <li><a href="https://luckly.work/categories/redis/">redis (2)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%B8%AA%E4%BA%BA%E8%B5%84%E6%96%99/">个人资料 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%B9%A6%E5%8D%95/">书单 (8)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%B9%A6%E8%AF%84/">书评 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E5%B8%88/">信息系统管理师 (19)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F%E9%A1%B9%E7%9B%AE%E7%AE%A1%E7%90%86%E5%B8%88/">信息系统项目管理师 (25)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%8E%9F%E5%88%99/">原则 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%8E%9F%E7%94%9F%E9%80%9A%E8%AE%AF/">原生通讯 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%9F%BA%E7%A1%80/">基础 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%A4%8D%E5%88%A9%E6%95%88%E5%BA%94/">复利效应 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%AE%89%E8%A3%85%E6%95%99%E7%A8%8B/">安装教程 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%B0%91%E6%9C%89%E4%BA%BA%E8%B5%B0%E7%9A%84%E8%B7%AF/">少有人走的路 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%BF%83%E8%AF%AD/">心语 (3)</a></li>
    
    <li><a href="https://luckly.work/categories/%E6%8F%92%E4%BB%B6/">插件 (2)</a></li>
    
    <li><a href="https://luckly.work/categories/%E6%95%99%E5%AD%A6/">教学 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%8E%8B%E9%98%B3%E6%98%8E/">王阳明 (3)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%94%B5%E5%AD%90%E4%B9%A6/">电子书 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%99%BB%E9%99%86%E8%A1%A8%E5%8D%95/">登陆表单 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%A8%BB%E7%9B%9B%E5%92%8C%E5%A4%AB/">稻盛和夫 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%A9%B7%E7%88%B8%E7%88%B8%E5%AF%8C%E7%88%B8%E7%88%B8/">穷爸爸富爸爸 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%B2%BE%E8%BF%9B/">精进 (3)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%BC%96%E7%A8%8B/">编程 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%99%9A%E5%B9%BB/">虚幻 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%B4%A2%E5%8A%A1%E8%87%AA%E7%94%B1%E4%B9%8B%E8%B7%AF/">财务自由之路 (2)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%B7%91%E6%AD%A5/">跑步 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%B7%AF%E7%94%B1%E4%BC%A0%E5%8F%82/">路由传参 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%B7%AF%E7%BA%BF/">路线 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%BD%AF%E4%BB%B6%E5%AE%9E%E6%96%BD/">软件实施 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E9%98%B3%E6%98%8E%E5%BF%83%E5%AD%A6/">阳明心学 (3)</a></li>
    
    <li><a href="https://luckly.work/categories/%E9%A1%B9%E7%9B%AE/">项目 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E9%AD%85%E5%8A%9B/">魅力 (1)</a></li>
    
</ul>
    </section>

    <section class="widget">
        <h3 class="widget-title"><a href='/tags/'>标签</a></h3>
<div class="tagcloud">
    
    <a href="https://luckly.work/tags/flutter/">flutter</a>
    
    <a href="https://luckly.work/tags/IT/">IT</a>
    
    <a href="https://luckly.work/tags/Kotlin/">Kotlin</a>
    
    <a href="https://luckly.work/tags/Mysql/">Mysql</a>
    
    <a href="https://luckly.work/tags/nginx/">nginx</a>
    
    <a href="https://luckly.work/tags/Vue/">Vue</a>
    
    <a href="https://luckly.work/tags/YouTube%E8%A7%86%E9%A2%91%E4%B8%8B%E8%BD%BD/">YouTube视频下载</a>
    
    <a href="https://luckly.work/tags/android/">android</a>
    
    <a href="https://luckly.work/tags/dart/">dart</a>
    
    <a href="https://luckly.work/tags/flutter/">flutter</a>
    
    <a href="https://luckly.work/tags/gin/">gin</a>
    
    <a href="https://luckly.work/tags/git/">git</a>
    
    <a href="https://luckly.work/tags/go/">go</a>
    
    <a href="https://luckly.work/tags/gorm/">gorm</a>
    
    <a href="https://luckly.work/tags/grpc/">grpc</a>
    
    <a href="https://luckly.work/tags/html/">html</a>
    
    <a href="https://luckly.work/tags/ios/">ios</a>
    
    <a href="https://luckly.work/tags/linux/">linux</a>
    
    <a href="https://luckly.work/tags/nginx/">nginx</a>
    
    <a href="https://luckly.work/tags/python/">python</a>
    
    <a href="https://luckly.work/tags/redis/">redis</a>
    
    <a href="https://luckly.work/tags/%E4%B8%AA%E4%BA%BA%E8%B5%84%E6%96%99/">个人资料</a>
    
    <a href="https://luckly.work/tags/%E4%B9%A6%E5%8D%95/">书单</a>
    
    <a href="https://luckly.work/tags/%E4%B9%A6%E8%AF%84/">书评</a>
    
    <a href="https://luckly.work/tags/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E5%B8%88/">信息系统管理师</a>
    
    <a href="https://luckly.work/tags/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F%E9%A1%B9%E7%9B%AE%E7%AE%A1%E7%90%86%E5%B8%88/">信息系统项目管理师</a>
    
    <a href="https://luckly.work/tags/%E5%85%A5%E9%97%A8/">入门</a>
    
    <a href="https://luckly.work/tags/%E5%8E%9F%E5%88%99/">原则</a>
    
    <a href="https://luckly.work/tags/%E5%8E%9F%E7%94%9F%E9%80%9A%E8%AE%AF/">原生通讯</a>
    
    <a href="https://luckly.work/tags/%E5%9F%BA%E7%A1%80/">基础</a>
    
    <a href="https://luckly.work/tags/%E5%A4%8D%E5%88%A9%E6%95%88%E5%BA%94/">复利效应</a>
    
    <a href="https://luckly.work/tags/%E5%AE%89%E8%A3%85%E6%95%99%E7%A8%8B/">安装教程</a>
    
    <a href="https://luckly.work/tags/%E5%B0%91%E6%9C%89%E4%BA%BA%E8%B5%B0%E7%9A%84%E8%B7%AF/">少有人走的路</a>
    
    <a href="https://luckly.work/tags/%E5%BF%83%E8%AF%AD/">心语</a>
    
    <a href="https://luckly.work/tags/%E6%8F%92%E4%BB%B6/">插件</a>
    
    <a href="https://luckly.work/tags/%E6%95%99%E5%AD%A6/">教学</a>
    
    <a href="https://luckly.work/tags/%E7%8E%8B%E9%98%B3%E6%98%8E/">王阳明</a>
    
    <a href="https://luckly.work/tags/%E7%94%B5%E5%AD%90%E4%B9%A6/">电子书</a>
    
    <a href="https://luckly.work/tags/%E7%99%BB%E9%99%86%E8%A1%A8%E5%8D%95/">登陆表单</a>
    
    <a href="https://luckly.work/tags/%E7%A8%BB%E7%9B%9B%E5%92%8C%E5%A4%AB/">稻盛和夫</a>
    
    <a href="https://luckly.work/tags/%E7%A9%B7%E7%88%B8%E7%88%B8%E5%AF%8C%E7%88%B8%E7%88%B8/">穷爸爸富爸爸</a>
    
    <a href="https://luckly.work/tags/%E7%B2%BE%E8%BF%9B/">精进</a>
    
    <a href="https://luckly.work/tags/%E7%BC%96%E7%A8%8B/">编程</a>
    
    <a href="https://luckly.work/tags/%E8%99%9A%E5%B9%BB/">虚幻</a>
    
    <a href="https://luckly.work/tags/%E8%AF%97/">诗</a>
    
    <a href="https://luckly.work/tags/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/">读书笔记</a>
    
    <a href="https://luckly.work/tags/%E8%B4%A2%E5%8A%A1%E8%87%AA%E7%94%B1%E4%B9%8B%E8%B7%AF/">财务自由之路</a>
    
    <a href="https://luckly.work/tags/%E8%B7%91%E6%AD%A5/">跑步</a>
    
    <a href="https://luckly.work/tags/%E8%B7%AF%E7%94%B1%E4%BC%A0%E5%8F%82/">路由传参</a>
    
    <a href="https://luckly.work/tags/%E8%B7%AF%E7%BA%BF/">路线</a>
    
    <a href="https://luckly.work/tags/%E8%BD%AF%E4%BB%B6%E5%AE%9E%E6%96%BD/">软件实施</a>
    
    <a href="https://luckly.work/tags/%E9%80%9A%E8%AE%AF%E5%BD%95/">通讯录</a>
    
    <a href="https://luckly.work/tags/%E9%98%B3%E6%98%8E%E5%BF%83%E5%AD%A6/">阳明心学</a>
    
    <a href="https://luckly.work/tags/%E9%A1%B9%E7%9B%AE/">项目</a>
    
    <a href="https://luckly.work/tags/%E9%AD%85%E5%8A%9B/">魅力</a>
    
</div>
    </section>

    
<section class="widget">
    <h3 class="widget-title">友情链接</h3>
    <ul class="widget-list">
        
        <li>
            <a target="_blank" href="http://www.topgoer.com/" title="枯藤">枯藤</a>
        </li>
        
        <li>
            <a target="_blank" href="https://gorm.cn/zh_CN/docs/index.html" title="gorm">gorm</a>
        </li>
        
        <li>
            <a target="_blank" href="https://docs.python.org/zh-cn/3/tutorial/index.html" title="python">python</a>
        </li>
        
        <li>
            <a target="_blank" href="https://www.liwenzhou.com/" title="李文周">李文周的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="http://www.xbzweb.com/" title="小包子的博客">小包子的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="https://www.flysnow.org/" title="飞雪无情的博客">飞雪无情的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="https://sliverhorn.com/" title="sliverhorn的博客">sliverhorn的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="http://yuedu.baidu.com/ebook/14a722970740be1e640e9a3e" title="Android Gradle权威指南">Android Gradle权威指南</a>
        </li>
        
        <li>
            <a target="_blank" href="https://gesdh.cn/" title="小格子">格子导航</a>
        </li>
        
        <li>
            <a target="_blank" href="https://itachi.xyz/" title="阿林">itachi&#39;s Blog</a>
        </li>
        
        <li>
            <a target="_blank" href="https://darjun.github.io/" title="大俊">大俊Blog</a>
        </li>
        
        <li>
            <a target="_blank" href="https://geektutu.com/post/quick-golang.html" title="极客兔兔">极客兔兔Blog</a>
        </li>
        
        <li>
            <a target="_blank" href="http://zxfcumtcs.github.io/" title="赵雪峰">雪峰Blog</a>
        </li>
        
    </ul>
</section>


    <section class="widget">
        <h3 class="widget-title">其它</h3>
        <ul class="widget-list">
            <li><a href="https://luckly.work/index.xml">文章 RSS</a></li>
        </ul>
    </section>
</div>
            </div>
        </div>
    </div>
</body>

</html>